package com.mayikt.aop.error;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.elk.kafka.KafkaSender;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

//@Aspect
//@ControllerAdvice
@Slf4j
public class ErrorExceptionHandler extends BaseApiService<JSONObject> {
    @Autowired
    private KafkaSender<JSONObject> kafkaSender;
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public JSONObject exceptionHandler(Exception e) {
        log.info("###全局捕获异常###,error:{}", e);
        JSONObject jsonObject = new JSONObject();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
        jsonObject.put("request_time", df.format(new Date()));
        jsonObject.put("error_info", e);
        kafkaSender.send(jsonObject);
//        返回错误信息
        JSONObject result = new JSONObject();
        result.put("code",500);
        result.put("msg","系统错误！");
        return result;
    }

}
